package com.gz.moreaseManager.sys.controller;

import com.gz.moreaseManager.core.base.Constants;
import com.gz.moreaseManager.core.modal.AjaxResult;
import com.gz.moreaseManager.modules.entity.*;
import com.gz.moreaseManager.modules.service.*;
import com.gz.moreaseManager.modules.transfer.*;
import com.gz.moreaseManager.utils.UtilTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @文件名: IndexController
 * @版权:Copyright 2018-2020版权所有：ganz
 * @描述: 首页控制层
 * @修改人: ganzheng
 * @修改时间: 10:42
 * @修改内容:新增
 */
@RestController
@RequestMapping("web")
public class IndexController {

    @Autowired
    private IMenuWebService menuWebService;

    @Autowired
    private IBannerService bannerService;

    @Autowired
    private IProductService productService;
    @Autowired
    private IProductTagService productTagService;
    @Autowired
    private IInformationService informationService;
    @Autowired
    private IRecruitService recruitService;
    @Autowired
    private IAssistInfoService assistInfoService;
    @Autowired
    private IAuthInfoService authInfoService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 官网首页
     *
     * @return 登录模板
     */
    @GetMapping("index")
    public ModelAndView index() {
        return new ModelAndView("web/index");
    }


    /**
     * 页面跳转
     *
     * @return 对应url页面
     */
    @GetMapping("/{url}")
    public ModelAndView tranferHtml( @PathVariable String url) {
        return new ModelAndView("web/"+url);
    }

    /**
     * 页面跳转
     *
     * @return 产品展示页面
     */
    @GetMapping("/productExhibition")
    public ModelAndView productExhibition(  Integer tagId ) {
        ModelAndView modelAndView = new ModelAndView("web/productExhibition");
        if(UtilTool.isNotBlank( tagId )){
            Banner banner = bannerService.findBannerByParams(Constants.BANNER_TYPE_PRODUCT,Constants.BANNER_POSITION_PRODUCT);
            if( UtilTool.isBlank( banner )) {
                banner = new Banner();
            }
            ProductTagListVo tagListVo = productService.findTagProductList(tagId);
            if( UtilTool.isBlank( tagListVo )) {
                tagListVo= new ProductTagListVo();
            }
            modelAndView.addObject("banner", banner);
            modelAndView.addObject("tagProductVo", tagListVo);
        }
        return modelAndView;
    }

    /**
     * 页面跳转
     *
     * @return 产品展示页面(同一系列)
     */
    @GetMapping("/productByTags")
    public ModelAndView productByTags(  Integer type ) {
        ModelAndView modelAndView = new ModelAndView("web/productByTags");
        if(UtilTool.isNotBlank( type )){
            Banner banner = bannerService.findBannerByParams(Constants.BANNER_TYPE_PRODUCT,Constants.BANNER_POSITION_PRODUCT);
            if( UtilTool.isBlank( banner )){
                banner = new Banner();
            }
            ProductTagListVo tagListVo = productService.findProductListByTags(type);
            if( UtilTool.isBlank( tagListVo )){
                tagListVo = new ProductTagListVo();
            }
            modelAndView.addObject("banner",banner);
            modelAndView.addObject("tagProductVo",tagListVo);
        }
        return modelAndView;
    }

    /**
     * 页面跳转
     *
     * @return 产品搜索结果
     */
    @GetMapping("/searchProduct")
    public ModelAndView searchProduct(  String queryName ) {
        ModelAndView modelAndView = new ModelAndView("web/searchProductList");
        List<ProductSimpVo> productList = productService.findSearchProductList(queryName);
        if( UtilTool.isBlank( productList ))
            productList = new ArrayList<ProductSimpVo>();
        modelAndView.addObject("productList",productList);
        return modelAndView;
    }

    /**
     * 页面跳转
     *
     * @return 产品展示页面
     */
    @GetMapping("/productDetails")
    public ModelAndView productDetails(  Integer productId ) {
        ModelAndView modelAndView = new ModelAndView("web/productDetails");
        ProductVo productVo = productService.findProductById( productId );
        String productMessage = productVo.getProductMessage();
        productMessage = productMessage.replace("\n", "<br/>");
        productMessage = productMessage.replace("\r", "<br/>");
        productMessage = productMessage.replace("/g", "<br/>");
        productMessage = productMessage.replace("\\s", "&nbsp;");
        productVo.setProductMessage(productMessage);
        modelAndView.addObject("product",productVo);
        //获取更多产品推荐
        List<ProductSimpVo> simpVos = productService.findProductRecommed( productVo.getTagIds() ,productVo.getProductId());
        //获取产品标签
        ProductTag productTag = productTagService.selectById(productVo.getTagIds().get(0));
        modelAndView.addObject("productTag",productTag);
        //随机获取标签信息
        ProductTag productTagRandom= productTagService.getRandomTag();
        modelAndView.addObject("productTagRandom",productTagRandom);
        modelAndView.addObject("simpPros",simpVos);

        return modelAndView;
    }

    /**
     * 页面跳转
     *
     * @return 资讯详情页面
     */
    @GetMapping("/information")
    public ModelAndView information(  Integer id ,Integer type) {
        ModelAndView modelAndView = new ModelAndView("web/information");
        InforVo inforVo = informationService.findInforVo( id,type );
        modelAndView.addObject("inforVo",inforVo);
        return modelAndView;
    }

    /**
     * 页面跳转
     *
     * @return 招聘详情页面
     */
    @GetMapping("/personDetails")
    public ModelAndView personDetails(  Integer id ) {
        ModelAndView modelAndView = new ModelAndView("web/personDetails");
        Recruit recruit = recruitService.selectById( id );
        modelAndView.addObject("recruit",recruit);
        return modelAndView;
    }

    /**
     * 页面跳转
     *
     * @return 活动详情页面
     */
    @GetMapping("/authActivityDetails")
    public ModelAndView authActivityDetails(  Integer id ) {
        ModelAndView modelAndView = new ModelAndView("web/authActivityDetails");
        AuthInfo authInfo = authInfoService.selectById( id );
        modelAndView.addObject("authInfo",authInfo);
        return modelAndView;
    }

    /**
     * 页面跳转
     *
     * @return 问题详情页面
     */
    @GetMapping("/problemsDetail")
    public ModelAndView problemsDetail(  Integer id ) {
        ModelAndView modelAndView = new ModelAndView("web/problemsDetail");
        AssistInfo assistInfo = assistInfoService.selectById( id );
        modelAndView.addObject("assistInfo",assistInfo);
        return modelAndView;
    }


    /**
     * 获取所有的一级主菜单
     * @return
     */
    @GetMapping("findIndexMenu")
    public AjaxResult<List<MenuWebVo>> findIndexMenu(){
        List<MenuWebVo> menuWebList = menuWebService.findIndexMenuList();
        return AjaxResult.success( menuWebList );
    }

    /**
     * 获取所有的一级主菜单
     * @return
     */
    @GetMapping("findUeditor")
    public ModelAndView test(){
        return new ModelAndView("ueditorDemo");
    }


}
